package com.tjetc.myservlet;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/1
 */
public class _3MyTomcat {

    // 第一个容器：存放的是Servlet实例
    public static final Map<String, MyHttpServlet> SERVLET_NAME_MAP = new ConcurrentHashMap<>();
    // 第二个容器：存放的是Servlet对应的请求路径
    public static final Map<String, String> SERVLET_URL_MAP = new ConcurrentHashMap<>();

    // 简化：Session容器 Filter容器

    public static void main(String[] args) {
        // Tomcat在启动时就会加载web.xml文件
        _3MyTomcat myTomcat = new _3MyTomcat();
        // 启动Tomcat
        myTomcat.run();
    }

    private void run() {
        // 初始化容器
        init();
        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("---------------tomcat启动 监听8080端口---------------");

            while (!serverSocket.isClosed()) {

                Socket socket = serverSocket.accept();

                // 创建请求处理器对象进行处理
                MyRequestHandler requestHandler = new MyRequestHandler(socket);
                // 开启线程进行处理
                new Thread(requestHandler).start();

            }
        } catch (Exception e) {

        }

    }


    private void init() {
        // 得到当前的类路径
        String path = _3MyTomcat.class.getResource("/").getPath();
//        System.out.println(path);
//        InputStream in = _3MyTomcat.class.getResourceAsStream("web.xml");

        SAXReader saxReader = new SAXReader();

        try {
            Document document = saxReader.read(new File(path + "web.xml"));
//            Document document = saxReader.read(in);
            Element rootElement = document.getRootElement();
//            System.out.println(rootElement);

            // 得到根元素下的所有子元素
            List<Element> elements = rootElement.elements();

            // 开始帅选servlet 和 url
            for (Element element : elements) {
                if ("servlet".equalsIgnoreCase(element.getName())) {
                    // servlet中有路径 可以通过反射来创建对应的实例
                    Element servletName = element.element("servlet-name");
                    Element servletClass = element.element("servlet-class");
                    SERVLET_NAME_MAP.put(servletName.getText(),
                            (MyHttpServlet) Class.forName(servletClass.getText().trim()).newInstance());
                } else if ("servlet-mapping".equalsIgnoreCase(element.getName())) {
                    Element servletName = element.element("servlet-name");
                    Element servletURI = element.element("url-pattern");
                    SERVLET_URL_MAP.put(servletURI.getText(), servletName.getText());
                }
            }
            System.out.println(SERVLET_NAME_MAP);
            System.out.println(SERVLET_URL_MAP);
        } catch (Exception e) {

        }


    }


}